﻿using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Script
{
    /// <summary>
    /// 用于供脚本访问组件的类
    /// </summary>
    public class FrameScriptObject : IDisposable
    {
        WeakReference _partFrame;

        private Dictionary<string, object> _dicVariables = new Dictionary<string, object>();

        public FrameScriptObject(PartFrame partFrame)
        {
            _partFrame = new WeakReference(partFrame);
        }

        /// <summary>
        /// 日期对象
        /// </summary>
        DateScriptObject _date;

        /// <summary>
        /// 获取日期对象
        /// </summary>
        public DateScriptObject date
        {
            get
            {
                if (_date == null)
                {
                    _date = new DateScriptObject();
                }
                return _date;
            }
        }

        /// <summary>
        /// 获取场景对象
        /// </summary>
        /// <returns></returns>
        public object getScene()
        {
            PartFrame partFrame = _partFrame.Target as PartFrame;
            if (partFrame == null)
            {
                return null;
            }
            object sceneObj = null;
            partFrame.Dispatcher.Invoke(new Action(() =>
            {
                ScenePart scene = partFrame.CurrentPart as ScenePart;
                if (scene != null)
                {
                    sceneObj = scene.GetScriptObj();
                }
            }));
            return sceneObj;
        }

        /// <summary>
        /// 打开场景
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="argumentString">参数字符串 a=1&b=2&c=3</param>
        public void openScene(string scene, string argumentString)
        {
            Coolzon.Event.NCL.NCLEventBus.PublishAsync(new Coolzon.Event.NCL.NCLOpenSceneEvent(SwitchMode.Replace, "", scene, OpenSceneArgs.BuildFromString(argumentString)));
        }

        /// <summary>
        /// 弹出场景
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="argumentString">参数字符串 a=1&b=2&c=3</param>
        public void popScene(string scene, string argumentString)
        {
            Coolzon.Event.NCL.NCLEventBus.PublishAsync(new Coolzon.Event.NCL.NCLOpenSceneEvent(SwitchMode.Pop, "", scene, OpenSceneArgs.BuildFromString(argumentString)));
        }

        public void alert(string message)
        {
            System.Windows.MessageBox.Show(message);
        }

        public void Dispose()
        {
            this._partFrame = null;
        }

        public void setVariable(string key, object value)
        {
            _dicVariables[key] = value;
        }

        public object getVariable(string key)
        {
            if (_dicVariables.ContainsKey(key))
            {
                return _dicVariables[key];
            }
            return null;
        }

        public object getVersion()
        {
            System.Reflection.Assembly entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
            return entryAssembly.GetName().Version.ToString(2);
        }
    }
}
